<?php namespace App\Services\TypesService;
/**
 * 主题API
 *
 * 主题Service
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
use App\Services\BaseService;
use App\Services\ProductsService\ProductsService;
use DB;
class TypesService extends BaseService{

    protected $producsService;
    public function __construct(ProductsService $productsService)
    {
        $this->producsService = $productsService;
    }


    /**
     * 根据一组id获取类型
     * @param $resource_ids
     * @return array
     */
    public function getListByReSourceIds($resource_ids){
        self::setParameter(array(
            'table' => 'types'
        ));
        $this->table = 'types';
        $this->select = array('type_id','type_name','type_image','app_id','is_features');
        $types = array();
        foreach($resource_ids as $resource_id){
            $this->where = array(array('type_id','=',$resource_id->resource_id),array('is_show','=',1),array('state','=',1));
            $type = $this->getById();
            if(!empty($type)){
                if(!empty($type->type_image)){
                    $type->type_image = $this->setImagePath($type->type_image);
                }
                $types[] = $type;
            }

        }
        return $types;
    }

    /**
     * 根据类型ID获取资源ID的集合
     * @param $type_id
     */
    public function getProductListByTypeId($type_id,$per_page = 10){
        //加标记不参与筛选(优化) 或者先筛选
        $product_ids = DB::table('products')->where([['is_shelves','=',0]])->orWhere([['state','=',2]])->orWhere([['is_show','<>',1]])->pluck('product_id')->toArray();
        $resource_ids = DB::table('type_resources')->where(['type_id'=>$type_id,'state'=>1])->whereNotIn('resource_id',$product_ids)
            ->select('resource_id')->orderBy('sort','desc')->paginate($per_page)->toArray();
        $products = $this->producsService->getListByReSourceIds($resource_ids['data']);
        return $this->createResult('M00000',array('total_page'=>$resource_ids['last_page'],'per_page'=>$per_page,
            'current_page'=>$resource_ids['current_page'],'data'=>$products));
    }

    /**
     * 获取主题列表
     * @param int $per_page
     * @return array
     */
    public function getList($per_page=10){
        //获取所有的主题
        $this->setParameter(array('table'=>'types','where'=>array(array('is_show','=',1),array('is_features','=',0),array('state','=',1)),'select'=>array('type_id','type_name','type_image','app_id'),'order'=>'sort','sort'=>'desc'));
        $types = $this->getAll()->toArray();
        foreach($types as $type){
            $type->type_image = $this->setImagePath($type->type_image);
        }
        $product_ids = DB::table('products')->where([['is_shelves','=',0]])->orWhere([['state','=',2]])->orWhere([['is_show','<>',1]])->pluck('product_id')->toArray();
        $resource_ids = DB::table('type_resources')->where(['state'=>1])->whereNotIn('resource_id',$product_ids)
            ->select('resource_id')->distinct()->orderBy('sort','desc')->orderBy('type_resource_id','desc')->paginate($per_page)->toArray();
        $products = $this->producsService->getListByReSourceIds($resource_ids['data']);
        return $this->createResult('M00000',array('total_page'=>$resource_ids['last_page'],'per_page'=>$per_page,'current_page'=>$resource_ids['current_page'],'data'=>array('types'=>$types,'products'=>$products)));
    }
}